import pandas as pd
import os

BASE_DATA_PATH="./insight_agent/mcp_server_graph/data/"

def excel_to_csv(excel_file, csv_file):
    """
    Convert an Excel file to a CSV file.
    """

    try:
        # 读取Excel文件
        df = pd.read_excel(excel_file)
        
        # 将DataFrame保存为CSV文件
        df.to_csv(csv_file, index=False, encoding='utf-8-sig')
        print(f"转换成功: {csv_file}")
    except Exception as e:
        print(f"转换失败: {e}")


def get_analyst_agent_input(user_input, cypher_result, purpose,vital_info=""):
    print("===============start================")
    input_length = len(str({
        "user_input": user_input,
        "cypher_result": cypher_result,
        "purpose": purpose,
        "vital_info":vital_info
    }))
    print("input json length: "+str(input_length))
    print("===============end================")

    # 超长就不传图结构了
    if input_length > 130000:
        return str({
        "user_input": user_input,
        "cypher_result": "请直接基于vital_info进行分析，不需要图结构",
        "purpose": purpose,
        "vital_info":vital_info
        })
    
    return str({
        "user_input": user_input,
        "cypher_result": cypher_result,
        "purpose": purpose,
        "vital_info":vital_info
        })

if __name__ == "__main__":
    # 定义Excel文件和CSV文件的路径
    excel_file = os.path.join(BASE_DATA_PATH+"excel", "用户订购产品表.xlsx")
    csv_file = os.path.join(BASE_DATA_PATH+"csv", "用户订购产品表.csv")
    
    # 调用转换函数
    excel_to_csv(excel_file, csv_file)

    
    


